package com.erp.erp_mask.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.erp.erp_mask.pojo.TPermissions;
import com.erp.erp_mask.service.TPermissionsService;
import com.erp.erp_mask.mapper.TPermissionsMapper;
import com.erp.erp_mask.vo.TreeData;
import com.erp.erp_mask.vo.TreeNode;
import com.sun.source.tree.Tree;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.*;

/**
 * @author yang
 * @description 针对表【t_permissions】的数据库操作Service实现
 * @createDate 2024-05-08 16:29:38
 */
@Service("tPermissionsServiceImpl")
public class TPermissionsServiceImpl extends ServiceImpl<TPermissionsMapper, TPermissions>
        implements TPermissionsService {

    private List<TPermissions> findPermission(Long parentid) {
        LambdaQueryWrapper<TPermissions> wrapper = Wrappers.lambdaQuery();
        if (parentid == null) {
            wrapper.isNull(TPermissions::getParentId);
        } else {
            wrapper.eq(TPermissions::getParentId, parentid)
                    .orderByAsc(TPermissions::getSort);
        }
        return baseMapper.selectList(wrapper);
    }


    //鉴权方法
    private boolean isAuth(TPermissions tPermissions) {
        SecurityContext sc = SecurityContextHolder.getContext();
        Authentication auth = sc.getAuthentication();
        //得到当前项目登录主题拥有的权限
        Collection<? extends GrantedAuthority> coll = auth.getAuthorities();
        SimpleGrantedAuthority authority = new SimpleGrantedAuthority(tPermissions.getPermCode());
        return coll.contains(authority);
    }

    @Override
    public Map<TPermissions, List<TPermissions>> findNavData() {
        Map<TPermissions, List<TPermissions>> navData = new TreeMap<>((p1, p2) -> p1.getSort() - p2.getSort());
        List<TPermissions> menu01 = this.findPermission(null)
                .stream().filter(this::isAuth).toList();
        menu01.stream().forEach(key -> {
            List<TPermissions> value = this.findPermission(key.getPermissionId())
                    .stream().filter(this::isAuth).toList();
            navData.put(key, value);
        });
        return navData;
    }

    @Override
    public Page<TPermissions> search(String name, Integer parentId, Long pageNo, Long pageSize) {
        return baseMapper.search(name, parentId, new Page(pageNo, pageSize));
    }

    @Override
    public List<TPermissions> searchPermid() {
        return baseMapper.searchPermid();
    }

    @Override
    public Integer searchSort(Integer parid) {
        return baseMapper.searchSort(parid);
    }

    @Override
    public void addList(TPermissions tPermissions) {
        baseMapper.addList(tPermissions);
    }

    @Override
    public TreeData findTreeData(Long roleId) {
        TreeData treeData = new TreeData();
        List<Long> ids = baseMapper.findDefaultCheckedKeys(roleId);
        treeData.setDefaultCheckedKeys(ids);
        List<TreeNode> list = findTreeNodeList(null);
        treeData.setData(list);
        return treeData;
    }

    @Override
    public List<TPermissions> searchTperlist() {
        List<TPermissions> tPermissionsList = this.findPermission(null);
        Map<Integer, TPermissions> tPermissionsMap = new HashMap<>();
        for (TPermissions per : tPermissionsList) {
            tPermissionsMap.put(Math.toIntExact(per.getPermissionId()), per);
        }
        for (TPermissions per : tPermissionsList) {
            List<TPermissions> children = this.findPermission(per.getPermissionId());
            per.setChildren(children);
            for (TPermissions child : children) {
                List<TPermissions> grandChildren = this.findPermission(child.getPermissionId());
                child.setChildren(grandChildren);
            }
        }
        return tPermissionsList;
    }

    private List<TreeNode> findTreeNodeList(Long parentId) {
        List<TPermissions> list = this.findPermission(parentId);
        List<TreeNode> treeNodes = this.transfer(list);
        return treeNodes;
    }

    private List<TreeNode> transfer(List<TPermissions> tPermissions) {
        if (tPermissions != null && tPermissions.size() > 0) {
            return tPermissions.stream().map(tPermissions1 -> {
                TreeNode treeNode = new TreeNode();
                treeNode.setId(tPermissions1.getPermissionId());
                treeNode.setLabel(tPermissions1.getPermissionName());
                List<TreeNode> children = findTreeNodeList(treeNode.getId());
                treeNode.setChildren(children);
                return treeNode;
            }).toList();
        }
        return null;
    }


}




